package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.GoodsVideoEntity;
import com.yimils.core.goods.entity.VideoEntity;
import com.yimils.core.goods.mapper.GoodsVideoMapper;
import com.yimils.core.goods.serivce.GoodsVideoService;
import com.yimils.core.goods.serivce.VideoService;
import com.yimils.core.goods.vo.GoodsVideoRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsVideoService")
public class GoodsVideoServiceImpl extends ServiceImpl<GoodsVideoMapper, GoodsVideoEntity> implements GoodsVideoService {
    @Resource(name = "videoService")
    VideoService videoService;

    @Override
    public Boolean saveGoodsVideo(List<String> videoUrls, Long goodsId) {
        List<VideoEntity> entities = videoService.saveVideoList(videoUrls);
        List<GoodsVideoEntity> collect = entities.stream().map(o -> {
            GoodsVideoEntity goodsVideoEntity = new GoodsVideoEntity();
            goodsVideoEntity.setVideoId(o.getId());
            goodsVideoEntity.setGoodsId(goodsId);
            goodsVideoEntity.setState(Boolean.FALSE);
            goodsVideoEntity.setCreateTime(LocalDateTime.now());
            goodsVideoEntity.setUpdateTime(LocalDateTime.now());
            return goodsVideoEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Boolean deleteGoodsVideo(List<Long> videoIds, Long goodsId) {
        QueryWrapper<GoodsVideoEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsVideoEntity::getVideoId, videoIds)
                .eq(GoodsVideoEntity::getGoodsId, goodsId)
                .eq(GoodsVideoEntity::getState, Boolean.FALSE);
        List<GoodsVideoEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });
        saveOrUpdateBatch(entities);
        List<GoodsVideoEntity> collect = videoIds.stream().map(o -> {
            GoodsVideoEntity goodsPictureEntity = new GoodsVideoEntity();
            goodsPictureEntity.setGoodsId(goodsId);
            goodsPictureEntity.setVideoId(o);
            goodsPictureEntity.setState(Boolean.FALSE);
            goodsPictureEntity.setCreateTime(LocalDateTime.now());
            goodsPictureEntity.setUpdateTime(LocalDateTime.now());
            return goodsPictureEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Map<Long, List<GoodsVideoRes>> mapListGoodsVideo(List<Long> goodsIds) {
        QueryWrapper<GoodsVideoEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsVideoEntity::getGoodsId, goodsIds)
                .eq(GoodsVideoEntity::getState, Boolean.FALSE);

        List<GoodsVideoEntity> videoEntities = list(qw);
        List<Long> videoIds = videoEntities.stream().map(GoodsVideoEntity::getVideoId).collect(Collectors.toList());
        List<VideoEntity> videoRes = videoService.listVideo(videoIds);
        Map<Long, String> videoMap = videoRes.stream().collect(Collectors.toMap(VideoEntity::getId, VideoEntity::getUrl));
        return videoEntities.stream().map(o -> {
            GoodsVideoRes res = new GoodsVideoRes();
            res.setGoodsId(o.getGoodsId());
            res.setVideoId(o.getVideoId());
            res.setUrl(videoMap.get(o.getVideoId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsVideoRes::getGoodsId));
    }
}
